package guesswords.khova.com;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import guesswords.khova.com.UIScrollBlock;

import android.view.KeyEvent;

public class GameBoardView extends BaseGameActivity
{
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	
	// Height and Width of the Game Block Pieces
	private static final int GAMEBLOCK_WIDTH = 49;
	private static final int GAMEBLOCK_HEIGHT = 44;
	private static final int NORMAL = 0;
	private static final int STARTUP = 1;
	
	private static final int SCROLLBLOCK_WIDTH = 25;
	private static final int SCROLLBLOCK_HEIGHT = 25;
	
	private static final char REDBLOCK = '@';
	private static final char BLUEBLOCK = '#';
	private static final char XAXIS = 'x';
	private static final char YAXIS ='y';
	
	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	private Texture mTexture;
	private Texture mTexture1;
	private TextureRegion mRedBlockTextureRegion;
	private TextureRegion mBlueBlockTextureRegion;
	private TextureRegion mTestTexture;
	
	private TiledTextureRegion mLetterBlockTextureRegion;
	private TiledTextureRegion mLetterScrollTextureRegion;
	
	private TextureRegion[] mScrollBlock = new TextureRegion[26]; 

	
	private GameBoard theGame; 
	private UIScrollBlock uiScrollBlock = new UIScrollBlock();
	
	
	
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public Engine onLoadEngine() {
		
		// Load New Game
		theGame = new GameBoard();
	
		// Set up Camera
		
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsSound(true));
	}
	
	@Override
	public void onLoadResources() {
		
		
		
		/* Load Game */
		theGame.loadPuzzle();
		
		/* Load the font we are going to use. */
		
		/* Load and Distribute GameLetters from Texture */
		this.mTexture = new Texture(512,512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mTexture1 =  new Texture(512,512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mLetterBlockTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "gfx/game_letter_blocks.png", 0, 0, 5,7);
		this.mLetterScrollTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture1, this, "gfx/gfx_letters.png",0,0,5,6);
		
		// Extract all TextureRegions needed.	
		this.mRedBlockTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,REDBLOCK,GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,REDBLOCK,GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		this.mBlueBlockTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,BLUEBLOCK,GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,BLUEBLOCK,GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		this.mTestTexture = TextureRegionFactory.extractFromTexture(this.mTexture1, 0, 0, 25, 25);		
		this.mEngine.getTextureManager().loadTextures( this.mTexture, this.mTexture1);
		uiScrollBlock.setTexture(this.mTexture1, this.mScrollBlock);
		
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		Sprite testSprite;

		//final Scene scene = new Scene(1);
	    Scene scene = new Scene(1);
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

		/* Add Data Board. */
		setGameBlocks(20,50);
		
		/* Draw Board to Screen */
		for (int x = 0; x < GameBoard.MAX_PIECES; x++){
		theGame.block[x].sprite = new Sprite (theGame.block[x].posX , theGame.block[x].posY, theGame.block[x].texture );		 
		scene.getLastChild().attachChild(theGame.block[x].sprite);
		}
		
	    scene = uiScrollBlock.loadDisplay(scene); // Build and Load the UI Scene
	    
	    // Setting update Update Handler
	    scene.registerUpdateHandler(new IUpdateHandler() {
	    	@Override
			public void onUpdate(final float pSecondsElapsed) {
	    		
	    		if (uiScrollBlock.checkRefresh())
	    		{
	    			refreshScene();
	  
	    		}
	    	}
	    	@Override
			public void reset() {}
	    });
		
		return scene;
				
	}
	


	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
	        if(pEvent.getAction() == KeyEvent.ACTION_DOWN)
	        {
	        	handleInput(pKeyCode,99);
	        	
	                switch(pKeyCode)
	                {      
			 
	                        case KeyEvent.KEYCODE_MENU:
	                        {
	                                //do something here
	                                return true; //this indicates that the key was processed       
	                        }
	                        case KeyEvent.KEYCODE_BACK:
	                        {
	                                //do something here
	                                return true;
	                        }
	                        case KeyEvent.KEYCODE_DPAD_LEFT:
	                        {
	                          	MLog.i(GuessWordz.TAG, "Left KEY PRESSED");
			                	uiScrollBlock.focusLetterDown();
			                	refreshScene();
			                	return true;
	                           
	                        }
	                        case KeyEvent.KEYCODE_DPAD_RIGHT:      
	                        {
	                          	MLog.i(GuessWordz.TAG, "Left KEY PRESSED");
			                	uiScrollBlock.focusLetterUp();
			                	refreshScene();
	                            return true;
	                        }
	                        case KeyEvent.KEYCODE_DPAD_UP:
	                        {
	                                //react on up key press
	                                return true;
	                        }
	                        case KeyEvent.KEYCODE_DPAD_DOWN:
	                        {
	                                //react on down key press
	                                return true;
	                        }
	                        default:
	                                return super.onKeyDown(pKeyCode, pEvent); //this will allow keypesses other than that to be processed in other places, for example by android OS
	                }
	                
	         	                
	        }
	        else
	                return super.onKeyDown(pKeyCode, pEvent); //similarily, this will allow actions other than key press to be processed elsewhere.
	}
	@Override
	public void onLoadComplete() {

	}
	


	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	
	/*
	 * Convert Character to Lowercase
	 */
	private char convertChar(char pchar)
	{
		char lower = Character.toLowerCase(pchar);
		
		return lower;
	}
	
	private void handleInput(int pKeyCode, int pLetterCode)
	{
		if ((pKeyCode == KeyEvent.KEYCODE_A) || (pLetterCode == 0))
		{
        	theGame.letterCheck('a');
        	MLog.i(GuessWordz.TAG, "A KEY PRESSED");
        	uiScrollBlock.setFocusLetter(0);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_B) || (pLetterCode == 1))
		{
        	theGame.letterCheck('b');
        	MLog.i(GuessWordz.TAG, "B KEY PRESSED");
        	uiScrollBlock.setFocusLetter(1);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_C) || (pLetterCode == 2))
		{
        	theGame.letterCheck('c');
        	MLog.i(GuessWordz.TAG, "C KEY PRESSED");
        	uiScrollBlock.setFocusLetter(2);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_D) || (pLetterCode == 3))
		{
        	theGame.letterCheck('d');
        	MLog.i(GuessWordz.TAG, "D KEY PRESSED");
        	uiScrollBlock.setFocusLetter(3);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_E) || (pLetterCode == 4))
		{
        	theGame.letterCheck('e');
        	MLog.i(GuessWordz.TAG, "E KEY PRESSED");
        	uiScrollBlock.setFocusLetter(4);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_F) || (pLetterCode == 5))
		{
        	theGame.letterCheck('F');
        	MLog.i(GuessWordz.TAG, "F KEY PRESSED");
        	uiScrollBlock.setFocusLetter(5);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_G) || (pLetterCode == 6))
		{
        	theGame.letterCheck('g');
        	MLog.i(GuessWordz.TAG, "G KEY PRESSED");
        	uiScrollBlock.setFocusLetter(6);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_H) || (pLetterCode == 7))
		{
        	theGame.letterCheck('h');
        	MLog.i(GuessWordz.TAG, "H KEY PRESSED");
        	uiScrollBlock.setFocusLetter(7);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_I) || (pLetterCode == 8))
		{
        	theGame.letterCheck('i');
        	MLog.i(GuessWordz.TAG, "I KEY PRESSED");
        	uiScrollBlock.setFocusLetter(8);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_J) || (pLetterCode == 9))
		{
        	theGame.letterCheck('j');
        	MLog.i(GuessWordz.TAG, "J KEY PRESSED");
        	uiScrollBlock.setFocusLetter(9);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_K) || (pLetterCode == 10))
		{
        	theGame.letterCheck('k');
        	MLog.i(GuessWordz.TAG, "K KEY PRESSED");
        	uiScrollBlock.setFocusLetter(10);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_L) || (pLetterCode == 11))
		{
        	theGame.letterCheck('l');
        	MLog.i(GuessWordz.TAG, "L KEY PRESSED");
        	uiScrollBlock.setFocusLetter(11);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_M) || (pLetterCode == 12))
		{
        	theGame.letterCheck('m');
        	MLog.i(GuessWordz.TAG, "M KEY PRESSED");
        	uiScrollBlock.setFocusLetter(12);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_N) || (pLetterCode == 13))
		{
        	theGame.letterCheck('n');
        	MLog.i(GuessWordz.TAG, "N KEY PRESSED");
        	uiScrollBlock.setFocusLetter(13);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_O) || (pLetterCode == 14))
		{
        	theGame.letterCheck('o');
        	MLog.i(GuessWordz.TAG, "O KEY PRESSED");
        	uiScrollBlock.setFocusLetter(14);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_P) || (pLetterCode == 15))
		{
        	theGame.letterCheck('p');
        	MLog.i(GuessWordz.TAG, "P KEY PRESSED");
        	uiScrollBlock.setFocusLetter(15);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_Q) || (pLetterCode == 16))
		{
        	theGame.letterCheck('q');
        	MLog.i(GuessWordz.TAG, "Q KEY PRESSED");
        	uiScrollBlock.setFocusLetter(16);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_R) || (pLetterCode == 17))
		{
        	theGame.letterCheck('r');
        	MLog.i(GuessWordz.TAG, "R KEY PRESSED");
        	uiScrollBlock.setFocusLetter(17);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_S) || (pLetterCode == 18))
		{
        	theGame.letterCheck('s');
        	MLog.i(GuessWordz.TAG, "S KEY PRESSED");
        	uiScrollBlock.setFocusLetter(18);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_T) || (pLetterCode == 19))
		{
        	theGame.letterCheck('t');
        	MLog.i(GuessWordz.TAG, "T KEY PRESSED");
        	uiScrollBlock.setFocusLetter(19);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_U) || (pLetterCode == 20))
		{
        	theGame.letterCheck('u');
        	MLog.i(GuessWordz.TAG, "U KEY PRESSED");
        	uiScrollBlock.setFocusLetter(20);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_V) || (pLetterCode == 21))
		{
        	theGame.letterCheck('v');
        	MLog.i(GuessWordz.TAG, "V KEY PRESSED");
        	uiScrollBlock.setFocusLetter(21);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_W) || (pLetterCode == 22))
		{
        	theGame.letterCheck('w');
        	MLog.i(GuessWordz.TAG, "W KEY PRESSED");
        	uiScrollBlock.setFocusLetter(22);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_X) || (pLetterCode == 23))
		{
        	theGame.letterCheck('x');
        	MLog.i(GuessWordz.TAG, "X KEY PRESSED");
        	uiScrollBlock.setFocusLetter(23);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_Y) || (pLetterCode == 24))
		{
        	theGame.letterCheck('y');
        	MLog.i(GuessWordz.TAG, "Y KEY PRESSED");
        	uiScrollBlock.setFocusLetter(24);
        	
		}
		
		if ((pKeyCode == KeyEvent.KEYCODE_Z) || (pLetterCode == 25))
		{
        	theGame.letterCheck('z');
        	MLog.i(GuessWordz.TAG, "Z KEY PRESSED");
        	uiScrollBlock.setFocusLetter(25);
        	
		}
		
		createSprite(NORMAL);
		
	}
	
	/*
	 * Coordinate Letter/Value with Position
	 */
	private int findLetterGFX(char axis, char theletter, int width, int height)
    {
        int xpos, ypos, value;
 
        switch (theletter)
        {
            case 'a':
                xpos = 0; ypos = 0;
                break;
            case 'b':
                xpos = width; ypos = 0;
                break;
            case 'c':
                xpos = width * 2; ypos = 0;
                break;
            case 'd':
                xpos = width * 3; ypos = 0;
                break;
            case 'e':
                xpos = width * 4; ypos = 0;
                break;
            case 'f':
                xpos = 0; ypos = height;
                break;
            case 'g':
                xpos = width; ypos = height;
                break;
            case 'h':
                xpos = width * 2; ypos = height;
                break;
            case 'i':
                xpos = width * 3; ypos = height;
                break;
            case 'j':
                xpos = width * 4; ypos = height;
                break;
            case 'k':
                xpos = 0; ypos = height * 2;
                break;
            case 'l':
                xpos = width; ypos = height * 2;
                break;
            case 'm':
                xpos = width * 2; ypos = height * 2;
                break;
            case 'n':
                xpos = width * 3; ypos = height * 2;
                break;
            case 'o':
                xpos = width * 4; ypos = height * 2;
                break;
            case 'p':
                xpos = 0; ypos = height * 3;
                break;
            case 'q':
                xpos = width; ypos = height * 3;
                break;
            case 'r':
                xpos = width * 2; ypos = height * 3;
                break;
            case 's':
                xpos = width * 3; ypos = height * 3;
                break;
            case 't':
                xpos = width * 4; ypos = height * 3;
                break;
            case 'u':
                xpos = 0; ypos = height * 4;
                break;
            case 'v':
                xpos = width; ypos = height * 4;
                break;
            case 'w':
                xpos = width * 2; ypos = height * 4;
                break;
            case 'x':
                xpos = width * 3; ypos = height * 4;
                break;
            case 'y':
                xpos = width * 4; ypos = height * 4;
                break;
            case 'z':
                xpos = 0; ypos = height * 5;
                break;
            case '-':
                xpos = width * 1; ypos = height * 5;
                break;
            case '/':
                xpos = width * 2; ypos = height * 5;
                break;
            case '\'':
                xpos = width * 3; ypos = height * 5;
                break;
            case ',':
                xpos = width * 4; ypos = height * 5;
                break;
            case REDBLOCK:
            	xpos = width * 1; ypos = height * 6;
            	break;
            case BLUEBLOCK:
            	xpos = width * 0; ypos = height * 6;
            	break;


            default:
                xpos = 0; ypos = 0;

                break;

        }
        if (axis == 'x')
        {
            value = xpos;
        }
        else
        {
            value = ypos;
        }
        return value;


    }
	
	/*
	 * Sprite Handling
	 */
	private void createSprite(int bypass)
	{
		//TODO Scrolling Letters at bottom of screen
		char theLetter;
		
		//************ Configure Texture for all the Blocks
		for (int c = 0; c < GameBoard.MAX_PIECES; c++)
        {
			if (theGame.block[c].isBlockVisible() == true)
			{
				theGame.block[c].texture = this.mBlueBlockTextureRegion;		
			}
			else
			{
				theGame.block[c].texture = this.mRedBlockTextureRegion;
			}
			
			if (theGame.block[c].isLetterVisible() == true)
			{
				theLetter = convertChar(theGame.block[c].dspValue());
			
				switch (theLetter)
		        {
		            case 'a':
		                theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'a',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'a',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		                MLog.i(GuessWordz.TAG, "'A' Texture Updated");
		                break;
		            case 'b':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'b',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'b',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		                break;
		            case 'c':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'c',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'c',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'd':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'d',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'d',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'e':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'e',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'e',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'f':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'f',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'f',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'g':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'g',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'g',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'h':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'h',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'h',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'i':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'i',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'i',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'j':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'j',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'j',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'k':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'k',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'k',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'l':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'l',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'l',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'm':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'m',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'m',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'n':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'n',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'n',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'o':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'o',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'o',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'p':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'p',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'p',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'q':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'q',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'q',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'r':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'r',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'r',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 's':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'s',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'s',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 't':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'t',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'t',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'u':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'u',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'u',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'v':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'v',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'v',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'w':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'w',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'w',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'x':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'x',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'x',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'y':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'y',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'y',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case 'z':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'z',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'z',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case '-':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'-',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'-',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case '/':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'/',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'/',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case '\'':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,'\'',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,'\'',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		            case ',':
		            	theGame.block[c].texture = TextureRegionFactory.extractFromTexture(this.mTexture, findLetterGFX(XAXIS,',',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), findLetterGFX(YAXIS,',',GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT), GAMEBLOCK_WIDTH, GAMEBLOCK_HEIGHT);
		
		                break;
		                
		        }
			
			}
			
        }
		
		if (bypass != STARTUP)
		{
		     //************ Runs through all the blocks and assign sprite
			Scene scene = this.mEngine.getScene();
			
			for (int c = 0; c < GameBoard.MAX_PIECES; c++)
			{
				 theGame.block[c].sprite = new Sprite (theGame.block[c].posX , theGame.block[c].posY, theGame.block[c].texture );
			 
				 scene.getLastChild().attachChild(theGame.block[c].sprite);
				 scene.registerTouchArea(theGame.block[c].sprite);
				
		                                                                        		 
			}
			
			//scene
			scene = uiScrollBlock.loadDisplay(scene);
			MLog.i(GuessWordz.TAG, "Scene Updated");
		
		}
	}
	
	/*
	 * Force a Scene Refresh
	 */
	private void refreshScene()
	{
		Scene scene = this.mEngine.getScene();
		//scene
		scene = uiScrollBlock.loadDisplay(scene);
		MLog.i(GuessWordz.TAG, "Scene Refresh Updated");
		
	//	handleInput(999, uiScrollBlock.chosenLetter);
		
	}
	
	/*
	 * Set up the gameblocks Texture based on game position and load
	 * the gameboard to memory
	 */
	private void setGameBlocks(int xpos, int ypos)
	{
		int x = xpos;
		int y = ypos;
		
		
		
		//************ Determine and Set the Position of the Blocks
        int i = 0;

        for (int row = 1; row <= GameBoard.BOARD_NUM_OF_ROWS; row++)
        {
            for (int col = 1; col <= GameBoard.BOARD_ROW_LENGTH; col++)
            {
                if (i < GameBoard.MAX_PIECES)
                {
                    theGame.block[i].posX = x;
                    theGame.block[i].posY = y;

                    x += GAMEBLOCK_WIDTH;
                    i++;

                }// END IF Statement
                
            }// End For Loop COL
            
            x = xpos;
            y += GAMEBLOCK_HEIGHT;
        }// END for Loop ROW
        
      createSprite(STARTUP);


	}// END setGameBlocks
	
}